package com.whoops.cloud.businessService.rpc;

import com.whoops.cloud.businessService.entity.Product;
import com.whoops.cloud.businessService.service.ProductService;
import com.whoops.cloud.inter.ProductApi;
import com.whoops.cloud.inter.entity.ProductDTO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ProductController implements ProductApi {

    @Autowired
    private ProductService productService;

    @ResponseBody
    public List<ProductDTO> getAllProducts(@RequestParam("type") String type){
        System.out.println("product1");
        List<Product> products = new ArrayList<>();
        if (StringUtils.equals(type,"domestic")){
            products = productService.getAllDomesticProducts();
        }else if (StringUtils.equals(type,"foreign")){
            products = productService.getAllForeignProducts();
        }
        List<ProductDTO> productDTOList = toProductDTOList(products);
        return productDTOList;
    }

    private List<ProductDTO> toProductDTOList(List<Product> productList){
        List<ProductDTO> productDTOList = new ArrayList<>();
        for (Product product : productList){
            productDTOList.add(product.getProductDTO());
        }
        return productDTOList;
    }


}
